package com.ruoyi.web.controller.danger;
import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.opio.util.ExampleBase;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.entity.SysEnvironmentPoint;
import com.ruoyi.system.service.SysEnvironmentPointService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * (环境检测)表控制层
 * @since 2022-12-01 16:50:30
 */
@RestController
@RequestMapping("sysEnvironmentPoint")
public class SysEnvironmentPointController extends BaseController {

    @Resource
    private SysEnvironmentPointService sysEnvironmentPointService;

    /**
     * 分页查询
     * @param sysEnvironmentPoint 筛选条件
     * @return 查询结果
     */
    @GetMapping("/list")
    public TableDataInfo queryByPage(SysEnvironmentPoint sysEnvironmentPoint) {
        startPage();
        List<SysEnvironmentPoint> sysEnvironmentPoints = this.sysEnvironmentPointService.queryByPage(sysEnvironmentPoint);
        return getDataTable(sysEnvironmentPoints);
    }

    /**
     * 通过主键查询单条数据
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public AjaxResult queryById(@PathVariable("id") Integer id) {
        return AjaxResult.success(this.sysEnvironmentPointService.queryById(id));
    }

    /**
     * 新增数据
     * @param sysEnvironmentPoint 实体
     * @return 新增结果
     */
    @PostMapping("/create")
//    @PreAuthorize("@ss.hasPermi('sysEnvironmentPoint:create')")
    public AjaxResult add(@RequestBody SysEnvironmentPoint sysEnvironmentPoint) {
        return AjaxResult.success(this.sysEnvironmentPointService.insert(sysEnvironmentPoint));
    }

    /**
     * 编辑数据
     * @param sysEnvironmentPoint 实体
     * @return 编辑结果
     */
    @PostMapping("/update")
//    @PreAuthorize("@ss.hasPermi('sysEnvironmentPoint:update')")
    public AjaxResult edit(@RequestBody SysEnvironmentPoint sysEnvironmentPoint) {
        return AjaxResult.success(this.sysEnvironmentPointService.update(sysEnvironmentPoint));
    }

    /**
     * 删除数据
     * @param id 主键
     * @return 删除是否成功
     */
    @GetMapping("/delete")
//    @PreAuthorize("@ss.hasPermi('sysEnvironmentPoint:delete')")
    public AjaxResult deleteById(Integer id) {
        return AjaxResult.success(this.sysEnvironmentPointService.deleteById(id));
    }

    /**
     * 与实时数据库关联
     * @return
     */
    @GetMapping("/getTimeData")
    public AjaxResult getTimeData(SysEnvironmentPoint query) {
        List<SysEnvironmentPoint> sysEnvironmentPoints = this.sysEnvironmentPointService.queryByPage(query);
        Map<String, Object> map = ExampleBase.toResult();
        if (CollUtil.isNotEmpty(map)) {
            sysEnvironmentPoints.forEach(sysEnvironmentPoint -> {
                if (map.containsKey(sysEnvironmentPoint.getBitNumber())) {
                    Object o = map.get(sysEnvironmentPoint.getBitNumber());
                    sysEnvironmentPoint.setAv(o.toString());
                }
            });
        }
        return AjaxResult.success(sysEnvironmentPoints);
    }



    @Log(title = "环境检测点位(", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception{
        ExcelUtil<SysEnvironmentPoint> util = new ExcelUtil<>(SysEnvironmentPoint.class);
        List<SysEnvironmentPoint> sysEnvironmentPoints = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = sysEnvironmentPointService.importExcel(sysEnvironmentPoints, updateSupport, operName);
        return success(message);
    }


    @Log(title = "环境检测点位(", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysEnvironmentPoint sysEnvironmentPoint)
    {
        List<SysEnvironmentPoint> sysEnvironmentPoints = sysEnvironmentPointService.queryByPage(sysEnvironmentPoint);
        ExcelUtil<SysEnvironmentPoint> util = new ExcelUtil<>(SysEnvironmentPoint.class);
        util.exportExcel(response, sysEnvironmentPoints, "环境检测点位(");
    }


    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<SysEnvironmentPoint> util = new ExcelUtil<>(SysEnvironmentPoint.class);
        util.importTemplateExcel(response,"环境检测点位(");
    }
}

